package com.mmlc.businessutils;

import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.OrderButtonBean;
import com.mmlc.meimei.lichengkongjian.dialog.CustomDialog;
import com.mmlc.network.filter.WantRefundRequestFilter;
import com.mmlc.utils.ToastUtils;

/**
 * Created by Administrator on 2016/8/16 0016.
 * 申请退款
 */
public class RequestBackMoneyRequest {

    private static final String TAG = "RequestBackMoneyRequest";
    private final OrderListUtils orderListUtils;
    private CustomDialog dialog;
    OrderButtonBean buttonBean;
    public RequestBackMoneyRequest(OrderListUtils orderListUtils){
        this.orderListUtils = orderListUtils;
        buttonBean = orderListUtils.buttonBean;
    }

    public void refund(final String orderId){
        View view = showEdit(R.layout.dialog_refund_reason);
        final RadioGroup rg_group = (RadioGroup) view.findViewById(R.id.rg_group);
        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                dialog.dialog.dismiss();

                RadioButton checkedView = (RadioButton) group.findViewById(checkedId);
                refundRequest(orderId,checkedView.getText().toString());
            }
        });
    }

    public View showEdit(int layoutId) {
        if (dialog == null) {
            dialog = new CustomDialog(buttonBean.activity);
        }
        dialog.setView(layoutId);
        dialog.show();
        return dialog.getContentView();
    }
    private void refundRequest(String orderId, String reason) {
        WantRefundRequestFilter filter = new WantRefundRequestFilter(buttonBean.activity);
        filter.offerErrorParams(buttonBean.rootView);
        filter.isTransparence = true;
        Log.e(TAG, "refundRequest: orderId"+orderId );
        filter.requestBean.order_id =orderId;
        filter.requestBean.reason =reason;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<String>() {

            @Override
            public void onSuccess(String result) {
                Log.e(TAG, "onSuccess: result"+result );
                ToastUtils.showToast("退款申请成功");
                 if(orderListUtils.u != null){
                    orderListUtils.u.updateUI(-1, OrderListUtils.UpdateUI.REFRESH);
                 }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast("退款申请失败");

            }
        });
    }
}
